package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 新增套餐
        setmealMapper.addSetmeal(setmeal);

        // 新增套餐菜品关系
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.insertSetmealDishBatch(setmealDishes);

    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    public void delSetmeals(ArrayList<Integer> ids) {

        // 起售中的套餐不能删除
        Integer countOnSale = setmealMapper.countSetmealOnSale(ids);
        if (countOnSale>0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        // 删除套餐
        setmealMapper.delSetmeals(ids);

        // 删除套餐所关联的菜品
        setmealDishMapper.delSetmealDishBatch(ids);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 修改套餐
        setmealMapper.updateSetmeal(setmeal);

        // 删除套餐所关联的菜品
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(setmealDTO.getId().intValue());
        setmealDishMapper.delSetmealDishBatch(ids);

        // 新增套餐所关联的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes.size()>0){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDTO.getId()));
            setmealDishMapper.insertSetmealDishBatch(setmealDishes);
        }
    }

    /**
     * 套餐起售、停售
     *
     * @param status
     * @param id
     */
    public void updateSetmealStatus(Integer status, Integer id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(Long.valueOf(id));

        Integer count = setmealMapper.countSetmealNoSaleDish(setmeal);
        if (status==1 && count > 0){
            throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setmealMapper.updateSetmealStatus(setmeal);
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageSetmeal(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageSetmeal(setmealPageQueryDTO);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据套餐id查询套餐
     *
     * @param id
     * @return
     */
    public SetmealVO getSetmealById(Integer id) {
        SetmealVO setmealVO = setmealMapper.getSetmealById(id);
        return setmealVO;
    }

    /**
     * C端-根据分类id查询在售套餐
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> getSetmealByCategoryId(Setmeal setmeal) {

        List<Setmeal> list = setmealMapper.getSetmealByCategoryId(setmeal);
        return list;
    }

    /**
     * 根据套餐id查询其包含的菜品
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishBySetmealId(Long id) {
        return setmealMapper.getDishBySetmealId(id);
    }
}
